<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/animate.min.css" />
    <link rel="stylesheet" href="./css/main.css" />
  </head>
  <body>
    <h1>hello model</h1>
    <button onclick="dialog.style.display='flex'">打开</button>
    <div class="model-mask" id="dialog">
      <div class="model-dialog animate__animated animate__backInDown">
        <div class="close" data-action="cancel">X</div>
        <div class="title">用户注册</div>

        <form name="f1">
          <p>姓名 <input type="text" name="uname" /></p>

          <p>
            性别
            <input type="radio" name="gender" value="1" checked /> 男
            <input type="radio" name="gender" value="2" /> 女
          </p>
          <p>
            爱好
            <input type="checkbox" name="hobbys" value="html" />HTML
            <input type="checkbox" name="hobbys" value="css" />CSS
            <input type="checkbox" name="hobbys" value="js" />JS
          </p>
          <p>
            城市
            <select name="hobby">
              <option value="nj">南京</option>
              <option value="sh">上海</option>
              <option value="wx">无锡</option>
            </select>
          </p>
          <p>
            头像
            <input type="file" multiple name="avatar" />
          </p>

          <p>
            <button type="button" data-action="save">提交</button>
            <button type="button" data-action="cancel">取消</button>
          </p>
        </form>
      </div>
    </div>
    <script>
      /*
        模态框 css+html
      */

      const f1 = document.f1;
      const dialog = document.getElementById("dialog");

      dialog.onclick = function (e) {
        const action = e.target.dataset.action;
        if (action === "cancel") {
          this.style.display = "none";
        }

        if (action === "save") {
          // 获取所有表单元素值
          // FormData 接受一个参数，参数必须是一个form对象，返回一个对象，这个对象中保存了多有的表单元素值
          const fd = new FormData(f1);

          console.log(fd);
          console.log(fd.get("uname"));
          console.log(fd.get("gender"));
          console.log(fd.getAll("hobbys"));
          console.log(fd.getAll("avatar"));
          console.log(fd.getAll("avatar"));

          // console.log(f1.uname.value);
          // console.log(f1.gender.value);
          // console.log(
          //   [...f1.hobbys].filter((el) => el.checked).map((el) => el.value)
          // );
          // console.log(f1.avatar.files);
        }
      };
    </script>
  </body>
</html>
